<#assign content>
<div class="right_col" role="main">
    <div class="">
        <div class="page-title">
            <div class="title_left">
                <h3>基础配方原料列表</h3>
            </div>
            <div class="title_right">
            </div>
        </div>
        <div class="clearfix"></div>
        <div class="row">
            <div class="col-md-12 col-sm-12 col-xs-12">
                <div class="x_panel">
                    <div class="x_title">
                        <h2>
                            <small></small>
                        </h2>
                        <ul class="nav navbar-right panel_toolbox">
                            <li><a id="basicFormulaAdd"><i class="fa fa-plus-circle"></i></a></li>
                            <li><a class="collapse-link"><i class="fa fa-chevron-up"></i></a>
                            </li>
                        </ul>
                        <div class="clearfix"></div>
                    </div>
                    <div class="x_content">
                    <#--信息显示-->
                        <div class="alert alert-info alert-dismissible fade in" role="alert">
                            <#if sBasics?exists><strong>基础配方名称：<#if sBasics.name?exists>${sBasics.name}</#if></strong></#if>
                        </div>
                        <div style="display: none"></div>
                    <#--配方原料-->
                        <#if basicMateList?exists && basicMateList?size gt 0>
                            <#list basicMateList as basicMateList>
                                <div class="col-md-4 col-sm-4 col-xs-12 profile_details">
                                    <div class="well profile_view" style="width: 100%">
                                        <div class="col-sm-12">
                                            <p><strong>原料名称：${basicMateList.mname}</strong></p>
                                            <p><strong>单位名称：${basicMateList.munit}</strong></p>
                                            <p><strong>消耗克数：${basicMateList.count}</strong></p>
                                        </div>
                                        <div class="col-xs-12 bottom text-center">
                                            <div class="col-xs-12 col-sm-12 emphasis">
                                                <button onclick="$.editBasicMate(${basicMateList.id})" type="button" class="btn btn-success btn-xs"><i class="fa fa-pencil"></i>修改</button>
                                                <button onclick="$.deleteBasicMate(${basicMateList.id})" type="button" class="btn btn-danger btn-xs"><i class="fa fa-trash-o"></i>删除</button>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </#list>
                        </#if>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</#assign>
<#include "../_inc/layout.ftl" />
<#--//增加配方参数-->
<div class="modal fade" id="basicFormulaAddModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
     aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content" style="width: 700px;">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel">添加配方原料</h4>
            </div>
            <form id="basicFormulaAddForm" method="post" novalidate class="form-horizontal form-label-left">
                <input type="hidden" id="add_basic_id" name="basicid" value="${sBasics.id}">
                <div class="modal-body">
                    <div class="item form-group">
                        <label class="control-label col-md-3">原料名称<span class="required">*</span>
                        </label>
                        <div class="col-md-5">
                            <select id="material" class="form-control col-md-6" required>
                                <option value="0">--请选择--</option>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="modal-body">
                    <div class="item form-group">
                        <label class="control-label col-md-3">原料克数<span class="required">*</span>
                        </label>
                        <div class="col-md-5">
                            <input type="text" id="addcount" name="addcount" data-validate-length-range="5,12"
                                   placeholder="请输入原料克数" required="required" class="form-control col-md-6">
                        </div>
                    </div>
                </div>
                <div class="ln_solid"></div>
                <div class="form-group">
                    <div class="col-md-8 col-md-offset-9">
                        <button id="saveBasicFormula" type="button" class="btn btn-success">保存</button>
                        <button class="btn btn-primary" type="reset">重置</button>
                    </div>
                </div>
            </form>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>

<#--原料明细修改-->
<div class="modal fade" id="machineMaterialEditModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
     aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content" style="width: 700px;">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel">原料明细修改</h4>
            </div>
            <form id="machineMaterialEditForm" method="post" novalidate class="form-horizontal form-label-left">
                <input type="hidden" id="edit_machineid" name="machineid">
                <input type="hidden" id="edit_machinematerialid" name="machinematerialid">
                <div class="modal-body">
                    <div class="item form-group">
                        <label class="control-label col-md-3">原料克数<span class="required">*</span>
                        </label>
                        <div class="col-md-5">
                            <input type="text" id="editamount" name="editamount" data-validate-length-range="5,12"
                                   placeholder="请输入原料克数" required="required" class="form-control col-md-6">
                        </div>
                    </div>
                </div>
                <div class="ln_solid"></div>
                <div class="form-group">
                    <div class="col-md-8 col-md-offset-9">
                        <button id="saveEditMachineMaterial" type="button" class="btn btn-success">保存</button>
                        <button class="btn btn-primary" type="reset">重置</button>
                    </div>
                </div>
            </form>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>

<#--// 删除配方中的配方参数-->
<div class="modal fade" id="basicFormulaDelete" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
     aria-hidden="true">

    <div class="modal-dialog modal-lg">
        <div class="modal-content" style="width: 700px;">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel">配方参数删除</h4>
            </div>
            <form id="basicFormulaDeleteForm" method="post" novalidate class="form-horizontal form-label-left">
                <input type="hidden" id="delete_basicid" name="id"  value="${sBasics.id}">
                <input type="hidden" id="detele_material_id" name="materialid">
                <div class="modal-body">
                    <div class="item form-group">
                        <div class="col-md-10">
                            <span id="basicFormulaDeleteMessage"></span>
                        </div>
                    </div>
                </div>
                <div class="ln_solid"></div>
                <div class="form-group">
                    <div class="col-md-8 col-md-offset-9">
                        <button id="saveDeleteBasicFormula" type="button" class="btn btn-success">确定</button>
                        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    </div>
                </div>
            </form>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>

<script>

    $(document).ready(function () {

        // 开启增加配方原料
        $('#basicFormulaAdd').on('click', function () {
            var param = {};
            param.basicid = $('#add_basic_id').val();
            $.ajax({
                type: 'POST',
                url: '/basicmate_add.do',
                cache: false,
                dataType: 'json',
                data: param,
                success: function (result) {
                    if (result.success) {
                        var materialist = result.body.materList;
                        for(var i = 0; i < materialist.length; i++) {
                            $('#material').append("<option value='"+materialist[i].mid+"'>"+materialist[i].mname+"</option>");
                        }
                        $('#basicFormulaAddModal').modal({
                            keyboard: true
                        });
                    }
                    else {
                        $.promptModal('${warning}', result.body.msg, result.logout);
                    }

                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    $.promptModal('${warning}', '${systemException}', false);
                }
            });
        });

        // 窗体关闭清空页面
        $('#basicFormulaAddModal').on('hide.bs.modal', function () {
            $('#basicFormulaAddForm')[0].reset();
        });


        // 保存新增配方原料
        $('#saveBasicFormula').on('click', function () {
            if (validator.checkAll($('#basicFormulaAddForm'))) {
                var param = {};
                param.action = 'do';
                param.basicid = $('#add_basic_id').val();
                param.materiaid = $('#material').val();
                param.matercount = $('#addcount').val();
                $('#basicFormulaAddForm')[0].reset();
                $('#basicFormulaAddModal').modal('hide');
                $.ajax({
                    type: 'POST',
                    url: '/basicmate_add.do',
                    cache: false,
                    dataType: 'json',
                    data: param,
                    success: function (result) {
                        if (result.success) {
                            var params = {};
                            params.basicid = $('#add_basic_id').val();
                            $.submitFormPost('/basicsmate_list.do', params,  'POST');
                        }
                        else {
                            $.promptModal('${warning}', result.msg, result.logout);
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        $.promptModal('${warning}', '${systemException}', false);
                    }
                });
            }
        });

        // 原料明细修改开启
        $.editBasicMate = function (id) {
            var param = {};
            param.action = 'edit';
            param.id = id;
            $.ajax({
                type: 'POST',
                url: '/basicmate_edit.do',
                cache: false,
                dataType: 'json',
                data: param,
                success: function (result) {
                    if (result.success) {
                        $('#edit_machineid').val(result.body.sBasicsFormula.bid);
                        $('#edit_machinematerialid').val(result.body.sBasicsFormula.id);
                        $('#editamount').val(result.body.sBasicsFormula.count);
                        $('#machineMaterialEditModal').modal({
                            keyboard: true
                        });
                    }
                    else {
                        $.promptModal('${warning}', result.msg, result.logout);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    $.promptModal('${warning}', '${systemException}', false);
                }
            });
        };

        // 原料保存修改
        $('#saveEditMachineMaterial').on('click', function () {
            if (validator.checkAll($('#machineMaterialEditForm'))) {
                var param = {};
                param.action = 'doEdit';
                param.basicmateid = $('#edit_machinematerialid').val();
                param.count = $('#editamount').val();
                $('#machineMaterialEditForm')[0].reset();
                $('#machineMaterialEditModal').modal('hide');
                $.ajax({
                    type: 'POST',
                    url: '/basicmate_edit.do',
                    cache: false,
                    dataType: 'json',
                    data: param,
                    success: function (result) {
                        if (result.success) {
                            var params = {};
                            params.basicid = $('#edit_machineid').val();
                            $.submitFormPost('/basicsmate_list.do', params, 'POST');
                        }
                        else {
                            $.promptModal('${warning}', result.msg, result.logout);
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        $.promptModal('${warning}', '${systemException}', false);
                    }
                });
            }
        });

        // 删除配方参数
        $.deleteBasicMate = function (id) {
            $('#detele_material_id').val(id);
            $('#basicFormulaDeleteMessage').html('确定删除此配方原料？');
            $('#basicFormulaDelete').modal({
                keyboard: true
            });
        };

        // 窗体关闭清空页面
        $('#basicFormulaDelete').on('hide.bs.modal', function () {
            $('#basicFormulaDeleteForm')[0].reset();
        });

        // 保存删除
        $('#saveDeleteBasicFormula').on('click', function () {
            var param = {};
            param.action = 'doDelete';
            param.basicmaid = $('#detele_material_id').val();
            $('#basicFormulaDelete').modal('hide');
            $.ajax({
                type: 'POST',
                url: '/basicmate_delete.do',
                cache: false,
                dataType: 'json',
                data: param,
                success: function (result) {
                    if (result.success) {
                        var params = {};
                        params.basicid = $('#delete_basicid').val();
                        $.submitFormPost('/basicsmate_list.do', params, 'POST');
                    }
                    else {
                        $.promptModal('${warning}', result.msg, result.logout);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    $.promptModal('${warning}', '${systemException}', false);
                }
            });
        });
    });
</script>

